package jp.co.sharp.android.parents.kidsguard.activity.setting.adapter;

import android.content.Context;
import android.support.annotation.NonNull;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.LinearLayout;
import android.widget.TextView;

import java.util.List;

import jp.co.sharp.android.parents.kidsguard.R;
import jp.co.sharp.android.parents.kidsguard.dto.WifiListDto;

public class DeviceSelectorAdaptor extends ArrayAdapter<WifiListDto> {
    private  int resourceId;
    private OnClickItemListenter onClickItemListenter;

    public DeviceSelectorAdaptor(@NonNull Context context, int resource) {
        super(context, resource);
        this.resourceId = resource;
    }

    public DeviceSelectorAdaptor(@NonNull Context context, int resource, int textViewResourceId) {
        super(context, resource, textViewResourceId);
        this.resourceId = resource;
    }

    public DeviceSelectorAdaptor(@NonNull Context context, int resource, @NonNull WifiListDto[] objects) {
        super(context, resource, objects);
        this.resourceId = resource;
    }

    public DeviceSelectorAdaptor(@NonNull Context context, int resource, int textViewResourceId, @NonNull WifiListDto[] objects) {
        super(context, resource, textViewResourceId, objects);
        this.resourceId = resource;
    }

    public DeviceSelectorAdaptor(@NonNull Context context, int resource, @NonNull List<WifiListDto> objects, OnClickItemListenter onClickItemListenter) {
        super(context, resource, objects);
        this.resourceId = resource;
        this.onClickItemListenter = onClickItemListenter;
    }

    public DeviceSelectorAdaptor(@NonNull Context context, int resource, int textViewResourceId, @NonNull List<WifiListDto> objects) {
        super(context, resource, textViewResourceId, objects);
        this.resourceId = resource;
    }
    @Override
    public View getView(int position, View converView, ViewGroup parent) {

        WifiListDto wifiListDto = getItem(position);

        View view;
        ViewHolder viewHolder;

        if (null == converView) {
            view = LayoutInflater.from(getContext()).inflate(resourceId, parent, false);

            viewHolder = new ViewHolder();
            viewHolder.wifiName = view.findViewById(R.id.wifiName);
            viewHolder.deviceItem = view.findViewById(R.id.deviceItem);
            view.setTag(viewHolder);
        } else {
            view = converView;
            viewHolder = (ViewHolder)view.getTag();
        }
        viewHolder.wifiName.setText(wifiListDto.getWifiId());
        viewHolder.deviceItem.setOnClickListener(new ChooseItemListenter());
        viewHolder.deviceItem.setTag(position);
        return view;
    }
    class ViewHolder{
        LinearLayout deviceItem;
        TextView wifiName;
    }
    public interface OnClickItemListenter {
        void chooseItemClick(int position, View view);
    }

    /*
     * 对外公开方法让Activity实现
     */
    public void OnDelBluetoothListenter(OnClickItemListenter callBack) {
        onClickItemListenter = callBack;
    }

    class ChooseItemListenter implements View.OnClickListener {

        @Override
        public void onClick(View view) {
            onClickItemListenter.chooseItemClick((Integer)view.getTag(), view);
        }
    }
}
































